﻿using System;
using System.Web.UI.WebControls;
using DataAccessObject;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.DB;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl.Admin
{
    public partial class UrlChangeUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Title_FormUC1.setTitle(ConstEx.Table_Name_C.Url_Change, ConstEx.funcButtonToolBar.LIST);
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
                //if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                //{
                //    pnlMain.Visible = false;
                //    Commons.setAccessDenied(Page, MsgUC1);
                //    return;
                //}
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                grvList.DataSource = (new UrlChangeDAO()).SelectList();
                grvList.DataBind();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                UrlChangeEO objTemp = new UrlChangeEO();
                objTemp.PK_iID = 0;
                objTemp.sUrlSrc = ((TextBox)grvList.FooterRow.FindControl("txtsUrlSrc_I")).Text;
                objTemp.sUrlDes = ((TextBox)grvList.FooterRow.FindControl("txtsUrlDes_I")).Text;
                objTemp.bStatus = ((CheckBox)grvList.FooterRow.FindControl("chkbStatus_I")).Checked;

                (new UrlChangeDAO()).Insert_Update_Delete(objTemp, 1);
                BindData();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                UrlChangeEO objTemp = new UrlChangeEO();
                LinkButton lbtnDelete = (LinkButton)sender;
                objTemp.PK_iID = Convert.ToInt32(lbtnDelete.CommandArgument);
                (new UrlChangeDAO()).Insert_Update_Delete(objTemp, 3);
                BindData();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void grvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void grvList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvList.EditIndex = -1;
            BindData();
        }

        protected void grvList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvList.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void grvList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                UrlChangeEO objTemp = new UrlChangeEO();
                objTemp.PK_iID = Convert.ToInt32(((Label)grvList.Rows[e.RowIndex].FindControl("lblPK_iID_V")).Text);
                objTemp.sUrlSrc = ((TextBox)grvList.Rows[e.RowIndex].FindControl("txtsUrlSrc_U")).Text;
                objTemp.sUrlDes = ((TextBox)grvList.Rows[e.RowIndex].FindControl("txtsUrlDes_U")).Text;
                objTemp.bStatus = ((CheckBox)grvList.Rows[e.RowIndex].FindControl("chkbStatus_U")).Checked;
                (new UrlChangeDAO()).Insert_Update_Delete(objTemp, 2);
                grvList.EditIndex = -1;
                BindData();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }
    }
}